package com.nullpointer.playerinfo.common;

import java.sql.SQLException;

import com.nullpointer.playerinfo.bungee.PlayerInfoBungee;
import com.nullpointer.playerinfo.common.exception.ExceptionEnum;
import com.nullpointer.playerinfo.common.exception.PlayerInfoException;
import com.nullpointer.playerinfo.common.objects.Player;
import com.nullpointer.playerinfo.common.objects.PluginMode;


/**
 * 此类中的方法非供外部使用
 * 请谨慎调用！！！
 */


public class Methods {
    /**
     * 仅供CMD类寻找目标玩家
     */
    public static Player getCmdTarget(String targetName, PluginMode pluginMode) throws PlayerInfoException {
        Player player = null;
        switch (pluginMode) {
            case BUKKIT:
                for (Player p : Base.playerMap.values()) {
                    if (targetName == p.getName()) {
                        player = p;
                        break;
                    }
                }
                if (player == null) {
                    try {
                        player = PlayerInfoBungee.mysql.getPlayer(targetName);
                    } catch (SQLException e) {
                        throw new PlayerInfoException(ExceptionEnum.PLAYER_NOFOUND);
                    }
                }
                break;
            case BUNGEE:
                for (Player p : Base.playerMap.values()) {
                    if (targetName == p.getName()) {
                        player = p;
                        break;
                    }
                }
                if (player == null) {
                    try {
                        player = PlayerInfoBungee.mysql.getPlayer(targetName);
                    } catch (SQLException e) {
                        throw new PlayerInfoException(ExceptionEnum.PLAYER_NOFOUND);
                    }
                }
                break;
            default:
                return null;
        }
        return player;
    }

    public static String[] getArgs(String message) {
        String[] args = message.split(" ");
        return args;
    }
}